我看过很多教程,但有些错误。我有简单的主动作,并且它的布局上有一个VideoView。我从youtube下载了一个视频,因为.mp4文件大小约为10 MB。我在youtube上找到了关于videoview的一个很好的示例,如以下链接。
https://www.youtube.com/watch?v=tPeDn18FrGY&t=75s
ublic类MainActivity扩展了AppCompatActivity {
private VideoView bgVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bgVideo=(VideoView) findViewById(R.id.bg_videoview);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bgvideo);
bgVideo.setVideoURI(uri);
bgVideo.start();
bgVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
}
我使用genymotion模拟器。当我运行应用程序时,我采取“无法播放此”错误。并在Android监视器上这样的事情。
06-07 03:16:28.675 2146-2146/com.sample E/MediaPlayer: Error (1,-2147483648)
06-07 03:16:28.675 2146-2146/com.sample D/VideoView: Error: 1,-2147483648
06-07 03:16:28.692 2146-2146/com.sample E/MediaPlayer: Error (1,-2147483648)
06-07 03:16:28.692 2146-2146/com.sample D/VideoView: Error: 1,-2147483648
06-07 03:16:28.709 2146-2146/com.sample E/MediaPlayer: Error (1,-2147483648)
06-07 03:16:28.709 2146-2146/com.sample D/VideoView: Error: 1,-2147483648
06-07 03:16:28.742 2146-2146/com.sample E/MediaPlayer: Error (1,-32)
06-07 03:16:28.742 2146-2146/com.sample D/VideoView: Error: 1,-32
06-07 03:16:28.758 2146-2146/com.sample E/MediaPlayer: Error (1,-38)
06-07 03:16:28.758 2146-2146/com.sample D/VideoView: Error: 1,-38
06-07 03:16:29.526 2146-2146/com.sample I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
我该如何解决?
答案 0 :(得分:1)
您可以使用MediaPlayer
对象并在一项服务中播放。这是示例代码:
public class Mp3PlayerService extends Service implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnSeekCompleteListener {
/** Android Built-in Media Player */
public MediaPlayer player;
/** Binder for Mp3 Service */
private final IBinder iBinder = new Mp3PlayerService.BackgroundAudioServiceBinder();
@Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
player.setOnCompletionListener(this);
player = new MediaPlayer();
try {
playSongLocal();
playSongNetwork();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private void playsongLocal() {
AssetFileDescriptor afd;
player.reset();
try {
LogUtils.LOGE(TAG, "playing aaa.mp3");
afd = getApplicationContext().getAssets().openFd("aaa.mp3");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
private void playsongNetwork() {
player.reset();
try {
player.setDataSource(networkUrlLink);
player.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void onDestroy() {
if (player.isPlaying()) {
player.stop();
}
player.release();
}
/** return itself when beeing asked */
public class BackgroundAudioServiceBinder extends Binder {
public Mp3PlayerService getService() {
return Mp3PlayerService.this;
}
}
答案 1 :(得分:0)
你需要在onPrepare
中启动bgVideo.startbgVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
bgVideo.start();
}
});
答案 2 :(得分:0)
试试这段代码:
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
MainActivity.this);
mediacontroller.setAnchorView(bgVideo);
// Get the URL from String VideoURL
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bgvideo);
bgVideo.setMediaController(mediacontroller);
bgVideo.setVideoURI(uri);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
bgVideo.requestFocus();
bgVideo.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
bgVideo.start();
}
});
答案 3 :(得分:0)
您是否尝试在onPreparedListener之后启动bgVideo?
bgVideo.setVideoURI(uri);
bgVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
bgVideo.start();
您还在使用哪些API版本?