6390ch00FM.qxd 3/24/06 4:13 PM Page i Pro Java ME MMAPI Mobile Media API for Java Micro Edition Vikram Goyal 6390ch00FM.qxd 3/24/06 4:13 PM Page ii Pro Java ME MMAPI: Mobile Media API for Java Micro Edition Copyright © 2006 by Vikram Goyal All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13: 978-1-59059-639-5 ISBN-10: 1-59059-639-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use atrademark symbol with every occurrence of atrademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: Robert Virkus Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Sofia Marchant Copy Edit Manager: Nicole LeClerc Copy Editor: Julie McNamee Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor and Artist: Kinetic Publishing Services, LLC Proofreader: Nancy Riddiough Indexer: Carol Burbo Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.comin the Source Code section. 6390ch00FM.qxd 3/24/06 4:13 PM Page iii In loving memory of my Papa,who would go to the ends of the Earth to find that one particular book.This one is for you. 6390ch00FM.qxd 3/24/06 4:13 PM Page iv 6390ch00FM.qxd 3/24/06 4:13 PM Page v Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii ■ CHAPTER 1 Introducing Mobile Media API (MMAPI) . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 MMAPI Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 ■ CHAPTER 3 Getting Started with MMAPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 ■ CHAPTER 4 Media Player Lifecycle and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 ■ CHAPTER 5 Accessing Media Over the Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 ■ CHAPTER 6 Creating and Playing Tones Using ToneControl. . . . . . . . . . . . . . . . . . 73 ■ CHAPTER 7 Managing MIDI Using MIDIControl,TempoControl,and PitchControl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 ■ CHAPTER 8 Working with Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 ■ CHAPTER 9 Case Study:Device Blogging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 ■ APPENDIX A Mobile Media API (MMAPI) Reference . . . . . . . . . . . . . . . . . . . . . . . . . 239 ■ APPENDIX B URI Syntax for Media Locators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■ APPENDIX C Advanced Multimedia Supplements—JSR 234 . . . . . . . . . . . . . . . . 251 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 v 6390ch00FM.qxd 3/24/06 4:13 PM Page vi 6390ch00FM.qxd 3/24/06 4:13 PM Page vii Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii ■ CHAPTER 1 Introducing Mobile Media API (MMAPI) . . . . . . . . . . . . . . . . . . . . 1 What Is Mobile Media API (MMAPI)? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 MMAPI Features and Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 How Do I Get MMAPI?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 How Does MMAPI Fit with MIDP 2.0? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Who Supports MMAPI?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 ■ CHAPTER 2 MMAPI Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Understanding Players and DataSources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 DataSource Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Player Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Understanding the Manager Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Creating Player Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Creating Player Instances for MIDI and Tone Sounds . . . . . . . . . . . . 11 Supported Protocols and Content Types . . . . . . . . . . . . . . . . . . . . . . . 11 Understanding Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The MIDP 2.0 Subset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Feature Set Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Security Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 ■ CHAPTER 3 Getting Started with MMAPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 A Simple Multimedia Player. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Creating a Functional Player. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Writing the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Improving AudioPlayerCanvas:Caching Player Instances. . . . . . . . . 29 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 vii 6390ch00FM.qxd 3/24/06 4:13 PM Page viii viii ■ CONTENTS ■ CHAPTER 4 Media Player Lifecycle and Events. . . . . . . . . . . . . . . . . . . . . . . . . 39 Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Exploring the Different Player States. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 UNREALIZED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 REALIZED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 PREFETCHED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 STARTED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 CLOSED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Responding to Player Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Understanding the Event Delivery Mechanism. . . . . . . . . . . . . . . . . . 50 Creating an Event Handling Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Handling a Custom Event. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 ■ CHAPTER 5 Accessing Media Over the Network . . . . . . . . . . . . . . . . . . . . . . . 59 Understanding Threads in Java ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Understanding Permissions for Network Access. . . . . . . . . . . . . . . . . . . . . 63 Putting It Together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 ■ CHAPTER 6 Creating and Playing Tones Using ToneControl. . . . . . . . . . . 73 Understanding Synthetic Tones. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Understanding Note,Pitch,and Frequency. . . . . . . . . . . . . . . . . . . . . 74 Using a MMAPI Formula to Calculate Note Values. . . . . . . . . . . . . . . 76 Using the playTone() Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Using Tone Sequences with ToneControl. . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Defining Tone Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Playing Sequences Using ToneControl and Player. . . . . . . . . . . . . . . 88 Distributing Tone Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 ■ CHAPTER 7 Managing MIDI Using MIDIControl,TempoControl, and PitchControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Understanding MIDI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Understanding the MIDI Specification . . . . . . . . . . . . . . . . . . . . . . . . . 96 Storing and Distributing MIDI Messages. . . . . . . . . . . . . . . . . . . . . . 101 6390ch00FM.qxd 3/24/06 4:13 PM Page ix ■ CONTENTS ix Using MIDI in MMAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Playing MIDI Without MIDIControl. . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Playing MIDI with MIDIControl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 ■ CHAPTER 8 Working with Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Querying the Capabilities of Your Device. . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Understanding Media Encodings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 A Brief Overview of Sampled Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Storing Sampled Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Controlling Sampled Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Setting Preset Stop Times with StopTimeControl. . . . . . . . . . . . . . . 134 Gathering Information Using MetaDataControl. . . . . . . . . . . . . . . . . 139 Mixing Sampled Audio,MIDI,and Tones . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Capturing Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Timed Capture and Playback. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Controlled Capture and Playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Saving Captured Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Capturing Audio from Existing Audio . . . . . . . . . . . . . . . . . . . . . . . . . 162 Working with Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Displaying Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Positioning Video and Controlling Volume. . . . . . . . . . . . . . . . . . . . . 169 Capturing Video and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Seeking Video Frames with FramePositioningControl. . . . . . . . . . . 181 Streaming Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 ■ CHAPTER 9 Case Study: Device Blogging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 About the Companion Web Site. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 The Finished MIDlet in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 MIDlet Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Registering with the Device Blog Web Site. . . . . . . . . . . . . . . . . . . . 186 Logging in to Device Blog Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Creating and Posting Blog Entries. . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Creating the MIDlet Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 The Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 The View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 The Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Description: