我想在点击图像时显示进度条,当视频准备播放或开始播放时,播放音频并停止进度条。
以下是图像和进度条的xml代码
<ImageView
android:id="@+id/img_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/buttonplayicon" />
<ProgressBar android:id="@+id/audio_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
onClick()
我正在调用函数 audioPlayer
public void audioPlayer(String path) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
MediaPlayer mp ;
mp = new MediaPlayer();
mp.setDataSource("SERVER_PATH/xyz");
//mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.prepareAsync();
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Downloading Music :) ");
dialog.dismiss();
}
同时将进度条的可见性设置为不可见我得到错误“Contructor progressDialog未定义” 有谁知道如何解决这个问题?
编辑1
Log-cat错误:
06-16 17:10:30.474: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:177, duration:3.011000, fps:58.766132
06-16 17:10:33.475: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:178, duration:3.001000, fps:59.307468
06-16 17:10:36.475: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:178, duration:3.000000, fps:59.328789
06-16 17:10:39.476: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:177, duration:3.000000, fps:58.982605
06-16 17:10:42.482: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:179, duration:3.006000, fps:59.537594
06-16 17:10:45.483: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:177, duration:3.000000, fps:58.982025
06-16 17:10:48.497: I/SurfaceTextureClient(13164): [0x50baf5f0] frames:176, duration:3.013000, fps:58.400032
06-16 17:10:50.244: V/Provider/Setting(13164): invalidate [system]: current 2 != cached 0
06-16 17:10:50.248: V/Provider/Setting(13164): from db cache, name = sound_effects_enabled value = 0
06-16 17:10:50.249: D/AndroidRuntime(13164): Shutting down VM
06-16 17:10:50.249: W/dalvikvm(13164): threadid=1: thread exiting with uncaught exception (group=0x411c0908)
06-16 17:10:50.253: E/AndroidRuntime(13164): FATAL EXCEPTION: main
06-16 17:10:50.253: E/AndroidRuntime(13164): java.lang.Error: Unresolved compilation problem:
06-16 17:10:50.253: E/AndroidRuntime(13164): Cannot make a static reference to the non-static method findViewById(int) from the type Activity
06-16 17:10:50.253: E/AndroidRuntime(13164): at com.example.paly_btn_imgapp.MainActivity$PlaceholderFragment.audioPlayer(MainActivity.java:131)
06-16 17:10:50.253: E/AndroidRuntime(13164): at com.example.paly_btn_imgapp.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:82)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.view.View.performClick(View.java:4091)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.view.View$PerformClick.run(View.java:17072)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.os.Handler.handleCallback(Handler.java:615)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.os.Handler.dispatchMessage(Handler.java:92)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.os.Looper.loop(Looper.java:153)
06-16 17:10:50.253: E/AndroidRuntime(13164): at android.app.ActivityThread.main(ActivityThread.java:5086)
06-16 17:10:50.253: E/AndroidRuntime(13164): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:10:50.253: E/AndroidRuntime(13164): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:10:50.253: E/AndroidRuntime(13164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-16 17:10:50.253: E/AndroidRuntime(13164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-16 17:10:50.253: E/AndroidRuntime(13164): at dalvik.system.NativeStart.main(Native Method)
编辑2
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
ImageView im = (ImageView)rootView.findViewById(R.id.img_play);
im.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
//showProgressbar();
audioPlayer("SERVER_PATH/xyz.mp3");
// prepareMediaPlayer();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return rootView;
}
public void audioPlayer(String path) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
MediaPlayer mp ;
mp = new MediaPlayer();
mp.setDataSource("SERVER_PATH");
//mp.prepare();
mp.setOnPreparedListener(new OnPreparedListener(){
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.prepareAsync();
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Downloading Music :) ");
dialog.dismiss();
}
}
答案 0 :(得分:0)
// Try this
public class MyProgressDialog extends Dialog {
public MyProgressDialog(Context context) {
super(context, R.style.Theme_CustomDialog_new);
super.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.row_progress);
this.setCancelable(false);
this.show();
}
}
//use this
MyProgressDialog pDialog = new MyProgressDialog("Your Activity Name");
pDialog.setCancelable(false);
pDialog.show();
//dismiss like
if (pDialog != null)
{
pDialog.dismiss();
}
答案 1 :(得分:0)
行ImageView im = (ImageView)rootView.findViewById(R.id.img_play);
产生错误,请尝试将其替换为
ImageView im = (ImageView)getActivity().findViewById(R.id.img_play);