我在 Android 上的音频播放器不工作 - Xamarin
My audio player on Android doesn't work - Xamarin
我使用 Xamarin 编写 C# Android 应用程序。我写了这段代码:
protected MediaPlayer player;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
player = new MediaPlayer();
player.Reset();
var fileDescriptor = Assets.OpenFd("MySound.mp3");
player.SetDataSource(fileDescriptor.FileDescriptor);
player.Prepare();
player.Start();
}
MySound.mp3 文件直接在Assets 文件夹中。当我 运行 应用程序出现错误时:
Java.IO.IOException
Message=Prepare failed.: status=0x1
与player.Prepare();
一致
怎么了?为什么它不起作用?
这似乎是 Android 设备上 MediaPlayer.Prepare() 的常见异常。快速搜索发现了这个:
匿名引用:
Hi, I wasted a lot of time, trying to find a solution to this issue on
my side too. So I post here, just in case it is helping some people.
My situation : I wanted to load an audio file from my assets (so not
registered in my resources). I am using a similar code as Ike Nwaogu,
except that I am using an AssetFileDescriptor to open my file (in my
activity class code, so I have access to "Assets") :
string path = "Audio/myfile.ogg";
Android.Content.Res.AssetFileDescriptor afd = Assets.OpenFd(path);
MediaPlayer soundPlayer = new MediaPlayer();
if (afd != null)
{
soundPlayer.Reset();
soundPlayer.SetDataSource(afd.FileDescriptor);
soundPlayer.Prepare();
soundPlayer.Enabled = true;
afd.Close();
}
I was failing on the Prepare(). I tried to add the access to external
storage permissions (but it did make sense since it
was loaded from my assets directly, I tried just in case).
Just by chance, by seeing other people samples on the forums, I added
the afd.StartOffset, afd.DeclaredLength to the parameters:
soundPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset,
afd.DeclaredLength);
and it worked ... I don't know if it just luck and if is going to fail
again later or if there is a bug in API ...
根据各种消息来源,在设置 DataSource 时使用 getStartOffset 和 getLength 应该可以解决这个问题:
player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
或者,这里还有一些想法:
Android MediaPlayer throwing "Prepare failed.: status=0x1" on 2.1, works on 2.2
https://forum.processing.org/two/discussion/6722/andriod-mediaplayer-prepare-failed-status-0x1
我使用 Xamarin 编写 C# Android 应用程序。我写了这段代码:
protected MediaPlayer player;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
player = new MediaPlayer();
player.Reset();
var fileDescriptor = Assets.OpenFd("MySound.mp3");
player.SetDataSource(fileDescriptor.FileDescriptor);
player.Prepare();
player.Start();
}
MySound.mp3 文件直接在Assets 文件夹中。当我 运行 应用程序出现错误时:
Java.IO.IOException Message=Prepare failed.: status=0x1
与player.Prepare();
怎么了?为什么它不起作用?
这似乎是 Android 设备上 MediaPlayer.Prepare() 的常见异常。快速搜索发现了这个:
匿名引用:
Hi, I wasted a lot of time, trying to find a solution to this issue on my side too. So I post here, just in case it is helping some people. My situation : I wanted to load an audio file from my assets (so not registered in my resources). I am using a similar code as Ike Nwaogu, except that I am using an AssetFileDescriptor to open my file (in my activity class code, so I have access to "Assets") :
string path = "Audio/myfile.ogg"; Android.Content.Res.AssetFileDescriptor afd = Assets.OpenFd(path); MediaPlayer soundPlayer = new MediaPlayer(); if (afd != null) { soundPlayer.Reset(); soundPlayer.SetDataSource(afd.FileDescriptor); soundPlayer.Prepare(); soundPlayer.Enabled = true; afd.Close(); }
I was failing on the Prepare(). I tried to add the access to external storage permissions (but it did make sense since it was loaded from my assets directly, I tried just in case).
Just by chance, by seeing other people samples on the forums, I added the afd.StartOffset, afd.DeclaredLength to the parameters:
soundPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.DeclaredLength);
and it worked ... I don't know if it just luck and if is going to fail again later or if there is a bug in API ...
根据各种消息来源,在设置 DataSource 时使用 getStartOffset 和 getLength 应该可以解决这个问题:
player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
或者,这里还有一些想法: Android MediaPlayer throwing "Prepare failed.: status=0x1" on 2.1, works on 2.2
https://forum.processing.org/two/discussion/6722/andriod-mediaplayer-prepare-failed-status-0x1