我有一个下载的MP3,我可以通过adb拉到我的mac或PC验证MP3,并在各自的媒体播放器中播放。但是,Android的错误意味着它不支持该格式。我认为它可能与我存储文件的目录位置有关,但这没有意义(/data/data/com.myapp/files/file_audio.mp3)。这是AsyncTask中的代码:
protected String doInBackground(String... params) {
String filename = params[0];
try {
StringBuilder filenameMp3 = new StringBuilder().append(context.getFilesDir());
filenameMp3.append("/").append(filename);
Uri uriMp3 = Uri.parse(filenameMp3.toString());
Log.d(TAG, "Listen to this ringtone! [" + uriMp3.toString() + "]");
MediaPlayer mp = MediaPlayer.create(context, uriMp3);
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
return null;
}
这是日志:
09-07 23:16:09.617 E/MediaPlayerService( 1128): The Default Player is PV_PLAYER***********************
09-07 23:16:09.617 V/PVPlayer( 1128): PVPlayer constructor
09-07 23:16:09.617 V/PVPlayer( 1128): construct PlayerDriver
09-07 23:16:09.617 V/PlayerDriver( 1128): constructor
09-07 23:16:09.627 V/PlayerDriver( 1128): OpenCore hardware module loaded
09-07 23:16:09.627 V/PlayerDriver( 1128): start player thread
09-07 23:16:09.627 V/PlayerDriver( 1128): startPlayerThread
09-07 23:16:09.627 V/PlayerDriver( 1128): InitializeForThread
09-07 23:16:09.627 V/PlayerDriver( 1128): OMX_MasterInit
09-07 23:16:09.647 V/PlayerDriver( 1128): OsclScheduler::Init
09-07 23:16:09.647 V/PlayerDriver( 1128): CreatePlayer
09-07 23:16:09.657 V/PlayerDriver( 1128): AddToScheduler
09-07 23:16:09.657 V/PlayerDriver( 1128): PendForExec
09-07 23:16:09.657 V/PlayerDriver( 1128): OsclActiveScheduler::Current
09-07 23:16:09.657 V/PlayerDriver( 1128): StartScheduler
09-07 23:16:09.657 V/PVPlayer( 1128): send PLAYER_SETUP
09-07 23:16:09.667 V/PlayerDriver( 1128): Send player code: 2
09-07 23:16:09.667 V/PlayerDriver( 1128): CommandCompleted
09-07 23:16:09.667 V/PlayerDriver( 1128): Completed command PLAYER_SETUP status=PVMFSuccess
09-07 23:16:09.667 V/PVPlayer( 1128): setDataSource(/data/data/com.myapp/files/audio.mp3)
09-07 23:16:09.667 V/PVPlayer( 1128): prepareAsync
09-07 23:16:09.667 V/PVPlayer( 1128): data source = /data/data/com.myapp/files/audio.mp3
09-07 23:16:09.667 V/PlayerDriver( 1128): Send player code: 3
09-07 23:16:09.667 V/PlayerDriver( 1128): handleSetDataSource
09-07 23:16:09.667 V/PlayerDriver( 1128): handleSetDataSource- scanning for extension
09-07 23:16:09.667 V/PlayerDriver( 1128): HandleInformationalEvent: PVMFInfoErrorHandlingStart
09-07 23:16:09.667 V/PlayerDriver( 1128): HandleInformationalEvent: type=26 UNHANDLED
09-07 23:16:09.667 W/MediaPlayer(14630): info/warning (1, 26)
09-07 23:16:09.667 I/MediaPlayer(14630): Info (1,26)
09-07 23:16:09.667 V/PlayerDriver( 1128): CommandCompleted
09-07 23:16:09.667 V/PlayerDriver( 1128): Completed command PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported
09-07 23:16:09.667 E/PlayerDriver( 1128): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
09-07 23:16:09.667 E/MediaPlayer(14630): error (1, -4)
09-07 23:16:09.677 V/PVPlayer( 1128): run_init s=-2147483648, cancelled=0
09-07 23:16:09.677 V/PlayerDriver( 1128): HandleInformationalEvent: PVMFInfoErrorHandlingComplete
09-07 23:16:09.677 W/PlayerDriver( 1128): PVMFInfoErrorHandlingComplete
任何线索?
答案 0 :(得分:2)
FileInputStream fis = getBaseContext().openFileInput(filename.toString());
mp = new MediaPlayer();
mp.setOnCompletionListener(onCompleteAudioListener);
mp.setDataSource(fis.getFD());
mp.prepare();
mp.start();
加上正常检查和错误处理。
答案 1 :(得分:0)
最好检查您要播放的文件是否存在且长度是否大于0只是为了确保路径正确:
File file = new File("/data/...../....mp3");
boolean exists = file.exists();
long length = file.length();
还要确保您的URI以file://
答案 2 :(得分:0)
好的 - 我得到了它的工作,但不是我希望我能实现的。首先,我必须存储在系统媒体文件夹中:
File file = new File("/system/media/audio/ringtones/", filename);
其次,我不得不停下来,准备,启动媒体播放器。我根据网络上其他地方的其他帖子尝试了这一点。
if (exists && length > 0) {
mp = MediaPlayer.create(context, uriMp3);
mp.stop();
mp.prepare();
mp.start();
} else {
if (exists) {
return String.format("Ringtone has length %d.", length);
} else {
return String.format("Ringtone %s is missing.", uriMp3.toString());
}
}