布衣王子

Email:roose2008@gmail.com QQ:79212131

NetBeans进行手机开发之——播放多媒体

       NetBeans使用mobility包开发手机程序,采用了流设计使得开发过程简便和可视化。在这里我主要介绍如何在使用mobility包开发播放多媒体数据(图片、音频、视频)的Demo,和大家一起分享。

 

       Demo开发环境:

              NetBeans 5.5.1

              Mobility Package

 

一.图片

NetBeans上手机显示图片非常方便,只需要几个拖拽和设置即可完成,甚至不需要写到一行代码。主要步骤为:

1. 把图片放到程序主目录/src/ 文件夹里面

2. 在资源中增加一个Image资源,并设置其属性,把资源路径属性设置为某个图片资源。

3. form中增加一个ImageItem,将其图片属性设为上述的Image资源,并根据需要设置图片的布局。

设置完运行程序即可发现图片已经可以显示。

 

二.音频

J2ME中,移动媒体APIMMAPI)是一个可选包,它提供了一个标准的API,可以用来表现和获得以时间为基础的媒体。例如声音轨道和视频片断。在mobility包安装后就会有这个MMAPI包,在程序->属性->可选包中即可发现MMAPI包。

在此Demo中使用的MMAPI1.1,这个版本支持以下媒体类型:

MIME类型                       描述

audio/midi                      MIDI文件

audio/sp-midi                   可升级的多音MIDI

audio/x-tone-seq                MIDP2.0音调序列

audio/x-wav                     WAV PCM取样音频

image/gif                       FIG89a(活动的GIF

video/mpeg                      MPEG视频

video/vnd.sun.rgb565            视频记录

其他媒体类型(Mp4,3gp)等现在还没支持,相信在不久的将来MMAPI新版本就会支持。

 

播放音频的主要开发步骤为:

1.       获得音频资源

2.       创建Player

3.       播放音频

主要的代码如下:

if (command == playMusicCommand) {                    

                // Insert pre-action code here

                isPlay = !isPlay;

                if(isPlay == true)

                {

                    form2.removeCommand(playMusicCommand);

                    playMusicCommand = new Command("暂停", Command.OK, 1);

                    form2.addCommand(playMusicCommand);

                    if(music == null)

                    {

                        try {                          

                            in = null;         //inInputStream类,此部分代码未显示

                                                 //获取音频资源,创建Player

                            in = getClass().getResourceAsStream("qhc.wav");

                            music = Manager.createPlayer(in,"audio/x-wav");

                           

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                        try {                

                            music.start();         //开始播放

                            ticker1.setString("正在播放音乐  ");

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                    else

                    {

                        try {

                            music.setMediaTime(pauseTime); //暂停后继续播放

                            music.start();

                            ticker1.setString("正在播放音乐 ");

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                }

                else

                {

                    try {

                        pauseTime = music.getMediaTime();    //暂停播放

                        music.stop();

                        ticker1.setString("暂停播放");

                        form2.removeCommand(playMusicCommand);

                        playMusicCommand = new Command("播放", Command.OK, 1);

                        form2.addCommand(playMusicCommand);

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

                // Do nothing                       

                // Insert post-action code here

            } else if (command == backCommand5) {                     

                // Insert pre-action code here

                try {

                    music.stop();         //停止播放

                    music.close();

                    isPlay = false;

                } catch (Exception e) {

                    e.printStackTrace();

                }

                form2.removeCommand(playMusicCommand);

                playMusicCommand = new Command("播放", Command.OK, 1);

                form2.addCommand(playMusicCommand);

                getDisplay().setCurrent(get_list1());                       

                // Insert post-action code here

            }

 

三.视频

播放视频仍然要注意播放的视频格式是否是正在使用的MMAPI版本所支持的,这里我们主要播放.mpg视频。

播放视频和播放音频的主要步骤是一样的,主要的区别在于视频需要建立VideoControl类,用来设置视频的显示模式等。

主要的代码如下:

if (command == playVideoCommand) {                    

                // Insert pre-action code here

                isPlay = !isPlay;

                if(isPlay == true)

                {

                    form3.removeCommand(playVideoCommand);

                    playVideoCommand = new Command("暂停", Command.OK, 1);

                    form3.addCommand(playVideoCommand);

                    //isPlay = false;

                    if(video == null)

                    {

                        try {                          

                            iv = null; //ivInputStream类,此部分代码未显示

                                                 //获取视频资源,创建Player

 

                            iv = getClass().getResourceAsStream("sun.mpg");

                            video = Manager.createPlayer(iv,"video/mpeg");

                            video.realize();

 

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                        try {

                                   // 设置VideoControl控制类               

                   VideoControl vc = (VideoControl)video.getControl("VideoControl");

  //设置视频播放的Item

Item videoItem = (Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,null);

                            //form3.deleteAll();

                            form3.append(videoItem);

                            video.start();          //开始播放

                            ticker2.setString("正在播放视频");

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                    else

                    {

                        try {

                            video.setMediaTime(pauseTime);  //暂停后继续播放

                            video.start();  

                            ticker2.setString("正在播放视频”");

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                }

                else

                {

                    try {

                        pauseTime = video.getMediaTime();     //暂停播放

                        video.stop();  

                        ticker2.setString("暂停播放视频 ");

                        form3.removeCommand(playVideoCommand);

                        playVideoCommand = new Command("播放", Command.OK, 1);

                        form3.addCommand(playVideoCommand);

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

               

                // Do nothing                       

                // Insert post-action code here

            } else if (command == backCommand4) {                     

                // Insert pre-action code here

                try {

                    video.stop();   //停止播放

                    video.close();

                    isPlay = false;

                } catch (Exception e) {

                    e.printStackTrace();

                }

                form3.removeCommand(playVideoCommand);

                playVideoCommand = new Command("播放", Command.OK, 1);

                form3.addCommand(playVideoCommand);

                getDisplay().setCurrent(get_list1());                        

                // Insert post-action code here             

            }

 

四.注意事项

开发播放多媒体程序时应该注意资源的大小和模拟器设置的大小以及开发应用的手机的资源等。在开发如果程序都还没有错误(如未释放内存资源等)还是出现OutOfMemoryError就应该模拟器设置的内存大小太小。

打开程序->属性->管理仿真器->工具和扩展->打开首选项->存储,可以设置存储大小和内存大小。但是此大小不能大于程序将应用的手机的内存大小,否则就算是程序能在模拟器中运行还是不能在手机中运行。

posted on 2008-06-20 10:43 草包书生 阅读(466) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

相册

java的相关连接

最新评论