Android声音未在初始屏幕中播放

编程入门 行业动态 更新时间:2024-10-10 21:23:13
本文介绍了Android声音未在初始屏幕中播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

显示初始屏幕时,我在播放声音时遇到麻烦.我已经在"res"目录下创建了"raw"目录,并将droid.mp3文件放在那里(大约150Kb).

Im having troubles with playing sound while the splash screen shows. Ive created the "raw" directory under "res" directory and put the droid.mp3 file there (around 150Kb).

这是负责启动屏幕的外观和声音的java文件的代码:

This is the code of the java file responsible for the appearance and sound of the splash screen:

import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; public class SplashActivity extends Activity { public MediaPlayer splashSound; protected void onCreate(Bundle splashBundle) { super.onCreate(splashBundle); setContentView(R.layout.splash); splashSound = MediaPlayer.create(SplashActivity.this, R.raw.droid); Thread t1 = new Thread() { public void run() { try { sleep(5000); } catch (InterruptedException IE) { IE.printStackTrace(); } finally { Intent mainActivityIntent=new Intent("com.example.stamapp.MAINACTIVITY"); startActivity(mainActivityIntent); } } }; t1.start(); } @Override protected void onPause() { super.onPause(); splashSound.release(); finish(); } }

非常感谢您的帮助.

推荐答案

而不是线程尝试使用 Handler.postDelayed 为:

instead of Thread try is using Handler.postDelayed as:

Handler handler; protected void onCreate(Bundle splashBundle) { super.onCreate(splashBundle); setContentView(R.layout.splash); handler = new Handler(); splashSound = MediaPlayer.create(SplashActivity.this, R.raw.droid); splashSound.start(); //<<<play sound on Splash Screen handler.postDelayed(runnable, 5000); } private Runnable runnable = new Runnable() { @Override public void run() { //start your Next Activity here } };

第二种方法是将 MediaPlayer.setOnCompletionListener 添加到MediaPlayer实例,该实例在媒体源播放完成时调用而没有将5000 Delay设置为:

and second way is add MediaPlayer.setOnCompletionListener to MediaPlayer instance which invoke when playback of a media source has completed without putting 5000 Delay as :

protected void onCreate(Bundle splashBundle) { super.onCreate(splashBundle); setContentView(R.layout.splash); splashSound = MediaPlayer.create(SplashActivity.this, R.raw.droid); splashSound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer splashSound) { splashSound.stop(); splashSound.release(); //start your Next Activity here } }); splashSound.start(); //<<<play sound on Splash Screen }

更多推荐

Android声音未在初始屏幕中播放

本文发布于:2023-11-12 13:24:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   声音   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!